using System;
using System.Collections.Generic;
using Castle.DynamicProxy;

namespace PropertyExpression.ControllableQuery.Test
{
    public static class ProxyCreator
    {
        public static T CreateInterfaceProxy<T>(IInterceptor interceptor)
        {
            return Cache<T>.Func(interceptor);
        }

        private static class Cache<T>
        {
            public readonly static Func<IInterceptor, T> Func;

            static Cache()
            {
                var options = ProxyGenerationOptions.Default;
                var type = new DefaultProxyBuilder().CreateInterfaceProxyTypeWithoutTarget(typeof(T), new Type[0], options);
                Func =
                    interceptor => (T)Activator.CreateInstance(
                        type,
                        new List<object>(options.MixinData.Mixins) { new[] { interceptor }, new object() }.ToArray());
            }
        }
    }
}